#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<mysql.h>
#include<string.h>

struct password
{
	char id[20];
	char passwd[20];
	int flag;
};

void ban(int connectfd,struct password pw)
{
    	MYSQL mysql;
    	MYSQL_RES *res = NULL;
    	char mysql_insert[100];
    	int fields;
    	MYSQL_ROW row;
    	int rows,rc,i=0,nflag=-1;
    	if(NULL == mysql_init(&mysql)) 
	{  
	        printf("mysql_init(): %s\n", mysql_error(&mysql));  
        	return -1;  
    	}  
    	if (NULL == mysql_real_connect(&mysql,  
			"localhost",  
                	"root",  
                	"root",  
                	"lts",  
                	 0,  
                	NULL,  
                	0)) 
	{  
        	printf("mysql_real_connect(): %s\n", mysql_error(&mysql));  
        	return -1;  
    	}  
    	sprintf(mysql_insert,"select * from user where username = %s",pw.id);
	rc = mysql_query(&mysql,mysql_insert);
	if(rc!=0)
	{
		printf("mysql_query(): %s\n", mysql_error(&mysql)); 
	}
    	res = mysql_store_result(&mysql);
    	if (NULL == res) 
	{
         	printf("mysql_store_result(): %s\n", mysql_error(&mysql));
         	return 0;
    	}
    	fields = mysql_num_fields(res);
   	while ((row = mysql_fetch_row(res))) 
	{
		if(strcmp(row[2],"0")!=0)    
		{
			sprintf(mysql_insert,"update user set fd = %d where username = '%s'",0,pw.id);
			if(mysql_query(&mysql,mysql_insert))
			{
				printf("mysql_query(): %s\n", mysql_error(&mysql)); 
			}
			else
			{
				nflag=1;
			}
		}
    	}		
	send(connectfd,&nflag,sizeof(int),0);
	mysql_close(&mysql); 
}
